package chapter06

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test07_Inherit
 * Package: chapter06
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-22
 *          Time: 19:26
 */

object Test07_Inherit {
  def main(args: Array[String]): Unit = {
    val stu1 = new Student7("alice", 18)
    val stu2 = new Student7("bob", 20, 209000111)

    val t = new Teacher
    val student = new Student7("jj",20)

    //多态 运行时绑定 Scala中 属性和方法都是运行时绑定 Java是只有方法是运行时候绑定
    def info(person: Person7): Unit = {
      person.printIndo()
    }
    info(stu1)
    info(t)
    info(student)

  }
}

//定义父类
class Person7() {
  //参数列表无参 主构造器是无参的
  var name: String = _
  var age: Int = _
  println("1.父类的主构造器调用")

  //创建辅助构造器
  def this(name: String, age: Int) {
    this() //调用主构造器
    println("2.父类的辅助构造器调用")
    this.name = name
    this.age = age
  }

  def printIndo(): Unit = {
    println(s"Person: ${name} ${age}")
  }

  //1.父类的主构造器调用
  //2.父类的辅助构造器调用
  //3.子类的主构造器调用
  //1.父类的主构造器调用
  //2.父类的辅助构造器调用
  //3.子类的主构造器调用
  //4.子类的辅助构造器调用
}

//定义子类 如果父类参数列表有参数(相当于就是调用了父类的主构造器)
//                        这里继承也要写参数 子类也要写参数
//子类的参数列表不需要写 var和val 因为继承了父类 父类已经定义过了
//如果继承给了参数 那么先把父类的调完在调子类的
class Student7(name: String, age: Int) extends Person7(name, age) {
  //
  var stdNo: Int = _
  //但是没有赋值
  println("3.子类的主构造器调用")

  def this(name: String, age: Int, stdNo: Int) {
    this(name, age)
    println("4.子类的辅助构造器调用")
    this.stdNo = stdNo
  }

  //重写父类的printInfo方法
  override def printIndo(): Unit = {
    println(s"name: ${name},age: ${age},stdNo: ${stdNo}")
  }

}

class Teacher extends Person7 {
  override def printIndo(): Unit = {
    println("teacher")
  }
}
